package com.iteye.weimingtom.japcoffee;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import android.content.ContentValues;
import android.content.Context;
import android.content.res.Resources;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.text.TextUtils;
import android.util.Log;

import com.iteye.weimingtom.japcoffee.R;

public class DictionaryOpenHelper extends SQLiteOpenHelper {
	private final static boolean D = true;
	private final static String TAG = "DictionaryOpenHelper";

    private final Context mHelperContext;
    private SQLiteDatabase mDatabase;

    private static final String FTS_TABLE_CREATE =
                "CREATE VIRTUAL TABLE " + DictionaryDatabase.FTS_VIRTUAL_TABLE +
                " USING fts3 (" +
                DictionaryDatabase.KEY_WORD + ", " +
                DictionaryDatabase.KEY_DEFINITION + ");";

    DictionaryOpenHelper(Context context) {
        super(context, DictionaryDatabase.DATABASE_NAME, null, DictionaryDatabase.DATABASE_VERSION);
        mHelperContext = context;
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        mDatabase = db;
        mDatabase.execSQL(FTS_TABLE_CREATE);
        loadDictionary();
    }

    private void loadDictionary() {
        new Thread(new Runnable() {
            public void run() {
                try {
                    loadWords();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }).start();
    }

    private void loadWords() throws IOException {
        if (D) {
        	Log.d(TAG, "Loading words...");
        }
        final Resources resources = mHelperContext.getResources();
        InputStream inputStream = resources.openRawResource(R.raw.definitions);
        BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));

        try {
            String line;
            while ((line = reader.readLine()) != null) {
                String[] strings = TextUtils.split(line, "-");
                if (strings.length < 2) continue;
                long id = addWord(strings[0].trim(), strings[1].trim());
                if (id < 0) {
                    if (D) {
                    	Log.e(TAG, "unable to add word: " + strings[0].trim());
                    }
                }
            }
        } finally {
            reader.close();
        }
        if (D) {
        	Log.d(TAG, "DONE loading words.");
        }
    }

    public long addWord(String word, String definition) {
        ContentValues initialValues = new ContentValues();
        initialValues.put(DictionaryDatabase.KEY_WORD, word);
        initialValues.put(DictionaryDatabase.KEY_DEFINITION, definition);

        return mDatabase.insert(DictionaryDatabase.FTS_VIRTUAL_TABLE, null, initialValues);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        if (D) {
        	Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
                + newVersion + ", which will destroy all old data");
        }
        db.execSQL("DROP TABLE IF EXISTS " + DictionaryDatabase.FTS_VIRTUAL_TABLE);
        onCreate(db);
    }
}

